package cn.lili.modules.wallet.manage;

import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.member.client.UserClient;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.payment.entity.dto.UserPointUpdateDTO;
import cn.lili.modules.promotion.client.GreenClient;
import cn.lili.modules.promotion.entity.dos.GreenConfig;
import cn.lili.modules.promotion.entity.dto.GreenConfigValueDTO;
import cn.lili.modules.promotion.entity.enums.GreenAmountInfoStatusEnum;
import com.alibaba.fastjson.JSON;
import com.api.PayApi;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @author xiangqian
 * @version 1.0
 * @data 2023/12/22 18:01
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class WalletMange {

    private final GreenClient greenClient;
    private final PayApi payApi;
    private final UserClient userClient;

    /**
     * 处理用户打款绿色积分
     * */
    public void memberGreen(UserPointUpdateDTO userPointUpdateDTO){
        if(userPointUpdateDTO.getScene().name().equals( SceneEnums.MEMBER.name())){
            //查询最新功能配置
            GreenConfigValueDTO greenConfigValueDTO = greenClient.getNewGreenConfigValueDTO();
            if(greenConfigValueDTO.getPutinto().getUser()){
                //兑换为贡献值
                greenClient.settle(userPointUpdateDTO.getOrderSn(),userPointUpdateDTO.getUserId());
            }else{
                //直接打款到飞语绿色积分
                paymentV2(userPointUpdateDTO.getUserId(),userPointUpdateDTO.getPoints(), StringUtils.isNotEmpty(userPointUpdateDTO.getOrderSn()) ? userPointUpdateDTO.getOrderSn() : "");
                greenClient.settleDirect(userPointUpdateDTO.getOrderSn(),userPointUpdateDTO.getUserId(), GreenAmountInfoStatusEnum.DIRECT.name());
            }
        }
    }

    /**
     * 处理商家打款绿色积分
     * */
    public void storeGreen(UserPointUpdateDTO userPointUpdateDTO){
        if(userPointUpdateDTO.getScene().name().equals( SceneEnums.STORE.name())){
            User user = userClient.getByExtId(userPointUpdateDTO.getUserId());
            if(user == null){
                return;
            }
            //查询最新功能配置
            GreenConfigValueDTO greenConfigValueDTO = greenClient.getNewGreenConfigValueDTO();
            if(greenConfigValueDTO.getPutinto().getStore() != null && greenConfigValueDTO.getPutinto().getStore()){
                //兑换为贡献值
                greenClient.settle(userPointUpdateDTO.getOrderSn(),user.getId());
            }else{
                //直接打款到飞语绿色积分
                paymentV2(user.getId(),userPointUpdateDTO.getPoints(), StringUtils.isNotEmpty(userPointUpdateDTO.getOrderSn()) ? userPointUpdateDTO.getOrderSn() : "");
                greenClient.settleDirect(userPointUpdateDTO.getOrderSn(),user.getId(), GreenAmountInfoStatusEnum.DIRECT.name());
            }
        }
    }

    /**
     * 处理代理商打款绿色积分
     * */
    public void supplierGreen(UserPointUpdateDTO userPointUpdateDTO){
        if(userPointUpdateDTO.getScene().name().equals( SceneEnums.SUPPLIER.name()) || userPointUpdateDTO.getScene().name().equals( SceneEnums.URBAN_SUBSTATION.name())){
            User user = userClient.getById(userPointUpdateDTO.getUserId());
            if(user == null){
                user = userClient.getByExtId(userPointUpdateDTO.getUserId());
            }
            if(user == null){
                return;
            }
            //查询最新功能配置
            GreenConfigValueDTO greenConfigValueDTO = greenClient.getNewGreenConfigValueDTO();
            if(greenConfigValueDTO.getPutinto().getAgent()!=null && greenConfigValueDTO.getPutinto().getAgent()){
                //兑换为贡献值
                greenClient.settle(userPointUpdateDTO.getOrderSn(),user.getId());
            }else{
                //直接打款到飞语绿色积分
                paymentV2(user.getId(),userPointUpdateDTO.getPoints(), StringUtils.isNotEmpty(userPointUpdateDTO.getOrderSn()) ? userPointUpdateDTO.getOrderSn() : "");
                greenClient.settleDirect(userPointUpdateDTO.getOrderSn(),user.getId(), GreenAmountInfoStatusEnum.DIRECT.name());
            }
        }
    }

    /**
     * 处理直推打款绿色积分
     * */
    public void lowerGreen(UserPointUpdateDTO userPointUpdateDTO){
        if(userPointUpdateDTO.getScene().name().equals( SceneEnums.LOWER.name())){
            //查询最新功能配置
            GreenConfigValueDTO greenConfigValueDTO = greenClient.getNewGreenConfigValueDTO();
            log.info("greenConfigValueDTO== {}", JSON.toJSONString(greenConfigValueDTO));
            if(greenConfigValueDTO.getPutinto().getLower() != null && greenConfigValueDTO.getPutinto().getLower()){
                //兑换为贡献值
                greenClient.settle(userPointUpdateDTO.getOrderSn(),userPointUpdateDTO.getUserId());
            }else{
                //直接打款到飞语绿色积分
                paymentV2(userPointUpdateDTO.getUserId(),userPointUpdateDTO.getPoints(),StringUtils.isNotEmpty(userPointUpdateDTO.getOrderSn()) ? userPointUpdateDTO.getOrderSn() : "");
                greenClient.settleDirect(userPointUpdateDTO.getOrderSn(),userPointUpdateDTO.getUserId(), GreenAmountInfoStatusEnum.DIRECT.name());
            }
        }

    }

    /**
     * 处理间推打款绿色积分
     * */
    public void lowerSubGreen(UserPointUpdateDTO userPointUpdateDTO){
        if(userPointUpdateDTO.getScene().name().equals( SceneEnums.LOWERSUB.name())){
            //查询最新功能配置
            GreenConfigValueDTO greenConfigValueDTO = greenClient.getNewGreenConfigValueDTO();
            if(greenConfigValueDTO.getPutinto().getLowerSub() != null && greenConfigValueDTO.getPutinto().getLowerSub()){
                //兑换为贡献值
                greenClient.settle(userPointUpdateDTO.getOrderSn(),userPointUpdateDTO.getUserId());
            }else{
                //直接打款到飞语绿色积分
                paymentV2(userPointUpdateDTO.getUserId(),userPointUpdateDTO.getPoints(),StringUtils.isNotEmpty(userPointUpdateDTO.getOrderSn()) ? userPointUpdateDTO.getOrderSn() : "");
                greenClient.settleDirect(userPointUpdateDTO.getOrderSn(),userPointUpdateDTO.getUserId(), GreenAmountInfoStatusEnum.DIRECT.name());
            }
        }
    }

    /**
     * 处理城主打款绿色积分
     * */
    public void memberCastellan(UserPointUpdateDTO userPointUpdateDTO){
        //查询最新功能配置
        GreenConfigValueDTO greenConfigValueDTO = greenClient.getNewGreenConfigValueDTO();
        if(greenConfigValueDTO.getPutinto().getCastellan()!=null && greenConfigValueDTO.getPutinto().getCastellan()){
            //兑换为贡献值
            greenClient.settle(userPointUpdateDTO.getOrderSn(),userPointUpdateDTO.getUserId());
        }else{
            //直接打款到飞语绿色积分
            paymentV2(userPointUpdateDTO.getUserId(),userPointUpdateDTO.getPoints(),StringUtils.isNotEmpty(userPointUpdateDTO.getOrderSn()) ? userPointUpdateDTO.getOrderSn() : "");
            greenClient.settleDirect(userPointUpdateDTO.getOrderSn(),userPointUpdateDTO.getUserId(), GreenAmountInfoStatusEnum.DIRECT.name());
        }
    }

    /**
     * 处理工会打款绿色积分
     * */
    public void memberUnion(UserPointUpdateDTO userPointUpdateDTO){
        //查询最新功能配置
        GreenConfigValueDTO greenConfigValueDTO = greenClient.getNewGreenConfigValueDTO();
        if(greenConfigValueDTO.getPutinto().getUnion()!=null && greenConfigValueDTO.getPutinto().getUnion()){
            //兑换为贡献值
            greenClient.settle(userPointUpdateDTO.getOrderSn(),userPointUpdateDTO.getUserId());
        }else{
            //直接打款到飞语绿色积分
            paymentV2(userPointUpdateDTO.getUserId(),userPointUpdateDTO.getPoints(), StringUtils.isNotEmpty(userPointUpdateDTO.getOrderSn()) ? userPointUpdateDTO.getOrderSn() : "");
            greenClient.settleDirect(userPointUpdateDTO.getOrderSn(),userPointUpdateDTO.getUserId(), GreenAmountInfoStatusEnum.DIRECT.name());
        }
    }

    /**
     * 打款到飞语绿色积分
     * */
    public void paymentV2(String userId,double points, String orderSn){
        User user = userClient.getById(userId);
        if(user == null){
            return;
        }
        //领取奖金，打款绿色积分
        String attach = GreenAmountInfoStatusEnum.DIRECT.description();//备注
        int tradeType = 1;//固定值
        int operationType = 103;//用户自定义
        BigInteger money = new BigDecimal(points).multiply(new BigDecimal(100)).toBigInteger();
        payApi.paymentV3(user.getOpenId(), attach, money.intValue(), tradeType, operationType, "GMONEY", orderSn + "_" + userId);
    }
}
